import axios from 'axios'

const api = axios.create({
    baseURL: 'http://localhost:3000/api', // 后端API地址
    timeout: 5000,
    headers: {
        'Content-Type': 'application/json'
    }
})

export interface LoginForm {
    username: string
    password: string
}

export interface RegisterForm extends LoginForm {
    confirmPassword: string
}

export const login = async (data: LoginForm) => {
    try {
        const response = await api.post('/auth/login', data)
        return response.data
    } catch (error: any) {
        throw error.response?.data || { message: '登录失败，请稍后重试' }
    }
}

export const register = async (data: RegisterForm) => {
    try {
        const response = await api.post('/auth/register', data)
        return response.data
    } catch (error: any) {
        throw error.response?.data || { message: '注册失败，请稍后重试' }
    }
} 